package com.kharthick.pwGame.sprites;

import java.awt.image.*;
import java.awt.*;
import java.awt.Component;
import java.util.HashMap;

import com.kharthick.game.Core;

public class SpriteFactory {
	// a hashmap of sprites we can use
	private static final HashMap<String,Sprite> sprites = new HashMap<String,Sprite>();
	//private static ImageFilter filter;
	
	/**
	 *  our factory retrieves the sprite if it's already been loaded
	 *  if it hasn't been loaded, we load it and then retrieves it
	 *  
	 *  This technique is known as Lazy Instantiation
	 * @param tracker used to make sure images load
	 */
	public static Sprite getSprite(String s, MediaTracker tracker) {
		if(sprites.containsKey(s)) {
			return sprites.get(s);
		} 
		return loadSprite(s, tracker);
	}
	
	public static Sprite getSprite(String s, int numberOfSprites, int dimensionX, int dimensionY, MediaTracker tracker) {
		if(sprites.containsKey(s)) {
			return sprites.get(s);
		} 
		return loadSprite(s, numberOfSprites, dimensionX, dimensionY, tracker);
	}
	
	/**
	 * Loads up a sprite, puts it in the HashMap
	 * @param s filename of the sprite to load
	 * @param tracker 
	 * @return newly created sprite
	 */
	public static Sprite loadSprite(String s, MediaTracker tracker) {
		Sprite sp = new Spr(s, tracker);
		sprites.put(s, sp);
		return sp;
	}
	
	public static Sprite loadSprite(String s, int numberOfSprites, int dimensionX, int dimensionY, MediaTracker tracker) {
		Sprite sp = new Spr(s, numberOfSprites, dimensionX, dimensionY, tracker);
		sprites.put(s, sp);
		return sp;
	}
	

}

/**
 * this class implements a sprite interface so you can use it to
 * draw sprites
 */
class Spr implements Sprite {
	private final Image img;
	private Image[] animation = null;
	private int numberOfSprites;
	private int j = 0;
	
	public Spr(String filename, MediaTracker tracker){
		img = Toolkit.getDefaultToolkit().getImage(filename);
		tracker.addImage(img,0);
		try {
			tracker.waitForID(0);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public Spr(String filename, int numberOfSprites, int dimensionX, int dimensionY, MediaTracker tracker){
		this.numberOfSprites = numberOfSprites;
		img = Toolkit.getDefaultToolkit().getImage(filename);
		animation = new Image[numberOfSprites];
		tracker.addImage(img,0);
		
		for (int i=0;i< numberOfSprites;i++){
			ImageFilter filter;
			filter = new CropImageFilter(i*dimensionX,0,dimensionX,dimensionY);
			animation[i] = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(img.getSource(),filter));
			tracker.addImage(animation[i],1);
		}
		
		try {
			tracker.waitForID(0);
			tracker.waitForID(1);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public Image getImage(){
		if(null == animation){
			return img;
		}
		else {
			j++;
			if(j == numberOfSprites){
				j = 0;
			}
			return animation[j];
		}
	}

/*	public Image getImage() {
		return img;
	}*/
	
	public Image getImage(int i){
		return animation[i];
	}
	
}
